package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

func f1(w http.ResponseWriter, r *http.Request) {
	// 返回客户端数据
	file, err := ioutil.ReadFile("F:/go-the-way-of-learning/day7/1http_server/demo.txt")
	if err != nil {
		w.Write([]byte(fmt.Sprintf("%v", err)))
	}
	w.Write(file)
}

func f2(w http.ResponseWriter, r *http.Request) {
	queryParam := r.URL.Query() // 识别请求url中的参数
	name := queryParam.Get("name")
	age := queryParam.Get("age")
	fmt.Println(name, age)
	fmt.Println(r.Method)               // 请求方式:GET 或 POST
	fmt.Println(ioutil.ReadAll(r.Body)) // 打印客户端发来的数据
	w.Write([]byte("ok"))
}
func main() {
	// 第一个参数相当于拼接的路径, 所以浏览器访问: http://127.0.0.1:9090/day7/http_server/
	http.HandleFunc("/day7/http_server/", f1)
	http.HandleFunc("/xxx/", f2)
	http.ListenAndServe("127.0.0.1:9090", nil)
}
